<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作为属性名的Symbol</title>
</head>
<body>
    <h1>作为属性名的Symbol</h1>

    <script>
        const mySymbol = Symbol();

        // 第一种写法
        let a = {};
        a[mySymbol] = 'Hello!';

        // 第二种写法
        let b = {
            [mySymbol]: 'Hello!'
        };

        // 第三种写法
        let c = {};
        Object.defineProperty(c, mySymbol, { value: 'Hello!' });

        // 获取
        console.log(a[mySymbol]); // "Hello!"
        console.log(b[mySymbol]); // "Hello!"
        console.log(c[mySymbol]); // "Hello!"
    </script>
</body>
</html> 